// pages/studyRecord/writing/wtiting.js---范文仿写作业界面
//获取应用实例
const app = getApp();
const httpUtils = require('../../../utils/httpUtils.js');
let tool = require('../../../utils/tool.js');
var WxParse = require('../../../wxParse/wxParse.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imitation: {},
    imageurl: 'https://yxpmaterial.oss-cn-beijing.aliyuncs.com/',
    is_last_writing: 0, //0无上次仿写，1有上次仿写
    count: 0, //剩余可上传次数
    id: 1, //范文id
    picturepath: '',
    tempFilePaths: 'https://yxpmaterial.oss-cn-beijing.aliyuncs.com/xiezuo_fangxie_scfx@2x.png', //作业图片临时图片路径
    isSubmitBtnDisabled: false,
    source: 0, //来源，默认为0从学习记录详情来；为1为从家庭作业列表跳转进来
    task_id: '', //任务id
    sort: '13', //类别,写作写一遍
    hasreview: 0, //0未复习，1已提交复习
    pic:'', //作业图片
    can_submit:0, //1 可以提交 0不可以提交
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    // let essay_id = options.essay_id;
    // that.setData({
    //   id: essay_id,
    //   source: options.source,
    // })
    //   //调接口，获取仿写要求
    //   that.getessayRequire();
    if (options) {
      that.setData({
        task_id: options.task_id,
        sort: options.sort,
        can_submit:options.can_submit,
      })
    }
    //获取复习数据
    that.getEssayReview();
  },


  //获取复习数据
  getEssayReview(){
    let that = this;
    wx.showLoading({
      title: '正在加载',
      mask: true
    })
    httpUtils.getEssayReview({
      access_token: app.globalData.studentInfo.access_token ? app.globalData.studentInfo.access_token : app.globalData.access_token,
      calendar_id: that.data.task_id,
      sort:that.data.sort,
    }).then((res) => {
      let temurl = res.data.pic;
      let last_writing = 0;
      if (!temurl) {
        last_writing = 0;
      } else {
        last_writing = 1;
      }
      that.setData({
        imitation: res.data.content,
        is_last_writing: last_writing,
        pic:res.data.pic
      })
      WxParse.wxParse('article', 'html', res.data.content.require, that, 5)
    });

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log(this.data.tempFilePaths)
  },

  /**
   * 查看上次仿写
   */
  looklast: function () {
    let that = this;
    //修改本句的imgurl为上次的仿写地址即可
    // let imgUrl = that.data.imitation.pic;
    let imgUrl = that.data.pic;
    //在新界面预览样例大图
    wx.previewImage({
      current: imgUrl,
      urls: [imgUrl],
    })
  },

  /**
   * 点击从相册或者拍照选择作业图片
   */
  uploadEssay: function () {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        var tempsrc = res.tempFilePaths[0];
        console.log("临时文件地址:", tempsrc);
        that.setData({
          tempFilePaths: tempsrc,
        })
        //上传作文图片
        that.uploadPic();
      },
    })
  },

  /**
   * 上传作文图片
   */
  uploadPic: function () {
    let that = this;
    wx.showLoading({
      title: '正在上传',
      mask: true
    })
    let bodyParam = {
      access_token: app.globalData.studentInfo.access_token ? app.globalData.studentInfo.access_token : app.globalData.access_token,
      type: that.data.tempFilePaths,
    }
    let rs = tool.getSign(bodyParam, getApp().globalData.key);
    let tempurl = getApp().globalData.apiURL + 'common/upload?sign=' + rs.sign;
    console.log(tempurl)
    wx.uploadFile({
      url: tempurl,
      filePath: that.data.tempFilePaths,
      name: 'file',
      formData: {
        'type': that.data.tempFilePaths,
      },
      success(res) {
        wx.hideLoading();
        let str=res.data.replace(/\n/g,"\\n").replace(/\r/g,"\\r");
        let result = JSON.parse(str);
        let path = result.data.path;
        console.log(path)

        that.setData({
          picturepath: path,
        })

        wx.showToast({
          title: '图片上传成功',
          icon: 'none',
          duration: 1000
        })
      },

    })

  },

  /**
   * 查看范文样例
   */
  lookExample: function () {
    let that = this;
    let imgUrl = that.data.imitation.imitation_url;
    //在新界面预览样例大图
    wx.previewImage({
      current: imgUrl,
      urls: [imgUrl],
    })
  },
  /**
   * 点击提交作业按钮
   */
  submitHomework: function () {
    let that = this;
    if (!that.data.picturepath) {
      wx.showToast({
        title: '请先上传作业图片',
        icon: 'none',
        duration: 1000
      })
      return;
    }
    //弹确认提交弹窗
    wx.showModal({
      title: '提示',
      content: '是否确认提交',
      success(res) {
        if (res.confirm) {
          that.submitAnswer();
        } else if (res.cancel) {
          console.log("点击了取消") 
        }
      }
    })
  },

  submitAnswer(){
    let that = this;
    wx.showLoading({
      title: '正在提交',
      mask: true
    })
    httpUtils.submitRWReview({
      access_token: app.globalData.studentInfo.access_token ? app.globalData.studentInfo.access_token : app.globalData.access_token,
      calendar_id: that.data.task_id,
      id:that.data.imitation.id,
      question_type: 4,  //1 阅读 4 写作
      sort:that.data.sort,
      pic: that.data.picturepath, //拍照图片地址
      voice:'',//录音文件地址
      score:'', //语音识别的分数
    }).then((res) => {
      wx.hideLoading();
      wx.showToast({
        title: '作业提交成功~',
        icon: 'success',
        duration: 1000
      })
  
      setTimeout(function () {
        wx.navigateBack({
          delta: -1
        })
      }, 1000)
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})